/*
  学习目标：创建store - 推荐写法
*/

import { legacy_createStore as createStore } from "redux";

// 2. 使用单独的常量, 定义state的初始值
const initState = {
  count: 99,
  msg: "hello",
};

// 1. 使用单独的函数, 定义reducer函数
function countReducer(state = initState, action) {
  // 3. 使用Switch语句, 简化if语句
  switch (action.type) {
    case "add":
      return { count: state.count + 1 };

    case "des":
      return { count: state.count - 1 };

    default:
      return state;
  }
}

const store = createStore(countReducer);

store.subscribe(() => {
  console.log("store.getState()  ----->  ", store.getState());
});

store.dispatch({ type: "des" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
store.dispatch({ type: "add" });
